<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_member{
	
	public $m_CI 		= null;
	
	public $m_arrUser   = array();
	public function __construct()
	{
		$this->m_CI = &get_instance();
		
		// 载入model
		$this->m_CI->load->model('account/account_m');
		
		// 载入session
		$this->m_CI->load->library('session');
		
		// 载入安全类
		
		$this->m_CI->load->library('security','core');

		
		// 不存在session 而存在cookies
		if ( ($this->m_CI->session->userdata('account_id') == false || $this->m_CI->session->userdata('token')== false ) && ($this->m_CI->input->cookie('account') != false)) 
		{
			$cookie_account = json_decode(htmlspecialchars_decode($this->m_CI->input->cookie('account'), ENT_COMPAT),true);
			
			$account_info = $this->m_CI->db->query("SELECT * FROM " . DB_PREFIX . "account WHERE id='" . (int)$cookie_account['id'] . "
			' AND token ='" . $this->m_CI->security->xss_clean($cookie_account['token'])."'")->result_array();
			
			if (!empty($account_info)) 
			{
				$this->m_arrUser = $account_info[0];
				
				$newSession = array(
						'account_id' =>(int)$cookie_account['id'],
						'token'          =>$cookie_account['token'],
						);
				$this->m_CI->session->set_userdata($newSession);
			}
			else 
			{
			
				$this->logout();
			}
			unset($account_info);
			unset($cookie_account);
			
		
			
		}
		
		// 如果存在session 话，以seesion 为准
		if ($this->m_CI->session->userdata('account_id') != false &&$this->m_CI->session->userdata('token') != false) 
		{
			$query = $this->m_CI->db->query("SELECT * FROM " . DB_PREFIX . "account  WHERE id = '" . (int)$this->m_CI->session->userdata('account_id') . "
			' AND token ='" . $this->m_CI->security->xss_clean($this->m_CI->session->userdata('token'))."'")->result_array();
			
			if (!empty($query)) 
			{
				$this->m_arrUser = $query[0];

				$this->m_arrUser['lastIP'] 		= $this->m_CI->security->xss_clean($this->m_CI->input->server('REMOTE_ADDR'));
				$this->m_arrUser['lastTime']    = time();
									
				$this->m_CI->db->query("UPDATE " . DB_PREFIX . "account SET lastIP = '" . $this->m_CI->security->xss_clean($this->m_CI->input->server('REMOTE_ADDR')) . "',
															   lastTime=NOW() WHERE id = '" . (int)$this->m_arrUser['id'] . "'");
	
			} else {
				
				$this->logout();
			}
			

		}
		
	}
	
	/**
	 * 登录成功后，设置用户的信息 于 session，cookies 和用户信息缓存中
	 * @param array $p_arrUser  登录成功后，用户的信息
	 */
	public function login($p_arrUser,$outTime = 0) 
	{
		// 用户信息缓存
  		$this->m_arrUser = $p_arrUser;
  		
  		// session
  		$newSession = array(
  				'account_id' => $p_arrUser['id'],
  				'token' => $p_arrUser['token'],
  				);
  		$this->m_CI->session->set_userdata($newSession);
  		
  		// cookies
  		$accout_data = array(
  				'id'	 => $p_arrUser['id'],
  				'email'	 => $p_arrUser['email'],
  				'name'	 => $p_arrUser['name'],
  				'avatar' => $p_arrUser['avatar'],
  				'token'	 => $p_arrUser['token'],
  		);
  		
  		
  		// 过期时间
  		if ($outTime == 0)
  		{
  			$expires = $this->m_CI->config->item('sess_expiration');
  		}
  		else 
  		{
  			$expires = $outTime;
  		}
  		
  		
  		setcookie("account",json_encode($accout_data), (int)(time()*1 + $expires),'/');
  		$this->m_arrUser = $p_arrUser;
  		
  	}
  	
  	
	/**
	 *  登出操作，将session 和cookies 和用户信息缓存置为空
	 */
	public function logout() 
	{
		
		if ($this->m_CI->session->userdata('account_id') != false)
		{
				$this->m_CI->session->unset_userdata('account_id');
		}
		
	if ($this->m_CI->session->userdata('token') != false)
		{
				$this->m_CI->session->unset_userdata('token');
		}

		$this->m_arrUser = array();
		
		setcookie("account", "", time() - 3600,'/');
  	}
  	
  	public function getUser()
  	{
  		return  $this->m_arrUser;
  	}
}
